<template>
  <comcard>
    <div slot="com_title">
      <el-row>
        <el-col :sm="12">
          <el-form ref="form" :model="shopObj" label-width="100px" v-if="shopObj.name">
            <el-form-item label="店铺名称">
              <el-input v-model="shopObj.name"></el-input>
            </el-form-item>
            <el-form-item label="店铺公告">
              <el-input type="textarea" v-model="shopObj.bulletin"></el-input>
            </el-form-item>
            <el-form-item label="店铺头像">
              <!-- 头像和店铺图片在图片上传之前的校验是一样的，成功之后不一样 -->
              <el-upload
                class="avatar-uploader"
                action="http://localhost:5000/shop/upload"
                :show-file-list="false"
                :on-success="headerSuccess"
                :before-upload="beforeAvatarUpload"
              >
                <img v-if="headerUrl" :src="headerUrl" class="avatar" />
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </el-form-item>
            <el-form-item label="店铺图片">
              <el-upload
                action="http://localhost:5000/shop/upload"
                list-type="picture-card"
                :on-preview="yulan"
                :on-remove="handleRemove"
                :file-list="fileList"
                :on-success="headerList"
                :before-upload="beforeAvatarUpload"
              >
                <i class="el-icon-plus"></i>
              </el-upload>
              <el-dialog :visible.sync="dialogVisible">
                <img width="100%" :src="dialogImageUrl" alt="" />
              </el-dialog>
            </el-form-item>
            <el-form-item label="配送费">
              <el-input v-model="shopObj.deliveryPrice"></el-input>
            </el-form-item>
            <el-form-item label="配送时间">
              <el-input v-model="shopObj.deliveryTime"></el-input>
            </el-form-item>
            <el-form-item label="配送描述">
              <el-input
                type="textarea"
                v-model="shopObj.description"
              ></el-input>
            </el-form-item>
            <el-form-item label="店铺评分">
              <el-input v-model="shopObj.score"></el-input>
            </el-form-item>
            <el-form-item label="销量">
              <el-input v-model="shopObj.sellCount"></el-input>
            </el-form-item>
            <el-form-item label="活动：">
                 <el-checkbox-group v-model="cks">
                    <el-checkbox label="单人精彩套餐" border></el-checkbox>
                    <el-checkbox label="VC无限橙果汁全场8折" border></el-checkbox>
                    <el-checkbox label="复选框 C" border></el-checkbox>
                </el-checkbox-group>
            </el-form-item>
            <el-form-item label="营业时间">
              <el-date-picker
                v-model="startdate"
                type="datetimerange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              >
              </el-date-picker>
            </el-form-item>

            <el-form-item>
              <el-button type="primary" @click="onSubmit">立即创建</el-button>
              <el-button>取消</el-button>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
    </div>
    <div slot="com_content"></div>
  </comcard>
</template>

<script>
import { api_shoplist, api_shopUpdate } from "../../api/shop";
import Comcard from "../../components/comCard";
export default {
  components: { Comcard },
  data() {
    return {
      shopObj: {
        name: "",
        bulletin: "",
        avatar: "",
        deliveryPrice: "",
        deliveryTime: "",
        description: "",
        score: "",
        sellCount: "",
        // supports: [],
        pics: "",
        date: "",
        minPrice: "",
      },
      dialogImageUrl: "",
      dialogVisible: false,
      fileList: [], //已经上传图片的列表
      headerUrl: "",
      imageUrl: "",
      startdate: [], //时间区间输入框的值
      cks: [],
    };
  },
  mounted() {
    this.query();
  },

  methods: {
    // 点击店铺图片预览按钮触发函数
    yulan(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    // 移出照片墙图片触发函数，移除就得移除数据库的pics的值
    handleRemove(file, fileList) {
      // filelist就是[{name:图片名},{name}],这种格式的，直接遍历所有{}
      this.shopObj.pics = fileList.map((item) => {
        // 删一个item元素少一个，只返回图片名称到pics数组
        return item.name;
      });
      // 删除一个更新一次fillist
      this.fileList = fileList;
      console.log(file, fileList);
    },
    // 当店铺图片上传成功时触发的函数
    headerList(res, file) {
      this.shopObj.pics.push(res.imgUrl);
      this.fileList.push({
        name: res.imgUrl,
        url: `http://localhost:5000/upload/shop/${res.imgUrl}`,
      });
    },
    async query() {
      let rst = await api_shoplist();
      // avatar这是后台头像名字，上传的是需要全路径的,拼接路径赋值给headerUrl
      this.headerUrl = `http://localhost:5000/upload/shop/` + rst.data.avatar;
      this.shopObj = rst.data;
      this.startdate = rst.data.date;
      this.cks = rst.data.supports;
      // pice是个数组，item就是数组内每个的元素，map有返回值并且返回的是对象
      this.fileList = rst.data.pics.map((item) => {
        // 返回的是对象，可以看官网，filelist的data值里面就是数组包含对象，并且里面有俩个值，所以要图片渲染就得照着写
        return { name: item, url: `http://localhost:5000/upload/shop/${item}` };
      });
      console.log(rst);
    },
    async onSubmit() {
      this.shopObj.date=JSON.stringify(this.startdate);
                this.shopObj.pics=JSON.stringify(this.shopObj.pics);
                this.shopObj.supports=JSON.stringify(this.cks);
               let rst= await api_shopUpdate(this.shopObj);
                let {code,msg}=rst;
                if(code==0){
                    this.$message.success(msg);
                }
    },
    // 头像上传成功触发函数
    headerSuccess(res, file) {
      console.log(res);
      // 后台只需要图片名称，所以需要上传头像后修改一下
      this.shopObj.avatar = res.imgUrl;
      // res的imgurl就是图片名称，所以需要拼接，不然单个图片名称显示不出来
      this.headerUrl = `http://localhost:5000/upload/shop/` + res.imgUrl;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg" || file.type === "image/png";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
  },
};
</script>

<style lang="less" scoped>
/deep/.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 150px;
  height: 150px;
  line-height: 150px;
  text-align: center;
}
.avatar {
  width: 150px;
  height: 150px;
  display: block;
}
</style>
